using System;
using Gadgeteer.Modules.Sytech.CameraProtocol;
using Microsoft.SPOT;

namespace Gadgeteer.Modules.Sytech.Camera
{
    /// <summary>
    /// Progress event args
    /// Contains total blocks
    /// Current Block and
    /// File size
    /// </summary>
    public class ProgressEventArg : EventArgs
    {
        /// <summary>
        /// Event Args property
        /// Download file size
        /// </summary>
        public int size { private set; get; }

        /// <summary>
        /// Event Args property
        /// The number of downl oad blocks in the file
        /// </summary>
        public int Blocks { private set; get; }

        /// <summary>
        /// The current block recieved
        /// </summary>
        public int blockReceived { private set; get; }

        /// <summary>
        /// Camera picture progress event args constructor.
        /// 
        /// Contains the 
        /// </summary>
        /// <param name="filesize">filesize of picture</param>
        /// <param name="totalBlocks">total number of file blocks</param>
        /// <param name="block">current block downloading</param>
        public ProgressEventArg(int filesize, int totalBlocks, int block)
        {
            size = filesize;
            Blocks = totalBlocks;
            blockReceived = block;

        }



    }

    /// <summary>
    /// Event Args for Picture events see <see cref="SerialCamera.CameraPictureReady"/>
    /// 
    /// Contains properties for the
    /// Image as a Gadgeteer.Picture
    /// The Resolution as an ImageSize
    /// And the endocding of the data in the Picture Buffer
    /// </summary>
    public class ImageEventArg : EventArgs
    {
        /// <summary>
        /// The Picture instance
        /// </summary>
        public Picture Image { private set; get; }

        /// <summary>
        /// The Image Resolution of the Image
        /// (Thumb,sVga, Vga)
        /// </summary>
        public ImageSize Resolution { private set; get; }

        /// <summary>
        /// The type of encoding for the picture raw buffer
        /// </summary>
        public Picture.PictureEncoding ImageFormat { private set; get; }

     
        /// <summary>
        /// EventtArgs constructor
        /// Pass in the image buffer, encoding and the resolution
        /// </summary>
        /// <param name="image">data buffer of the image</param>
        /// <param name="encoding">the encoding used for the image buffer</param>
        /// <param name="resolution">the resolution of the image</param>
        public ImageEventArg(byte[] image,Picture.PictureEncoding encoding,ImageSize resolution)
        {
            Resolution = resolution;
            ImageFormat = encoding;
            Image = new Picture(image,encoding);
         
        }

        /// <summary>
        /// Get the image as a bitmap object
        /// 
        /// If the conversion fails, then the returned value will be null,
        /// error is logged if module debug logging turned on
        /// </summary>
        /// <returns></returns>
        public Bitmap GetBitmap()
        {
            Bitmap bmp = null;
            try
            {
                bmp = Image.MakeBitmap();
                
            }
            catch (Exception)
            { 
                Debug.Print("ERROR: image to bitmap conversion exception");
            }

            return bmp;
        }

        /// <summary>
        /// Get the raw image data buffer.
        /// Data will be in the format of ImageFormat
        /// </summary>
        /// <returns></returns>
        public byte[] GetImageDataBuffer()
        {
            return Image.PictureData;
        }
    
    }
    

    /// <summary>
    /// Camera progress delegate
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="arg"></param>
    public delegate void PictureProgressDel(object sender, ProgressEventArg arg);
}
